package com.dwx.gmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dwx.gmall.common.result.Result;
import com.dwx.gmall.model.product.BaseAttrInfo;
import com.dwx.gmall.model.product.BaseAttrValue;
import com.dwx.gmall.product.service.BaseAttrInfoService;
import com.dwx.gmall.product.service.BaseAttrValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "平台属性管理")
@RestController
@RequestMapping("/admin/product")
public class BaseAttrInfoController {

    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseAttrValueService baseAttrValueService;

    @ApiOperation(value = "根据商品分类获取平台属性")
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>> getAttrInfoList(
            @ApiParam(value = "一级分类",required = true)
            @PathVariable("category1Id") Long category1Id,
            @ApiParam(value = "二级分类",required = true)
            @PathVariable("category2Id") Long category2Id,
            @ApiParam(value = "三级分类",required = true)
            @PathVariable("category3Id") Long category3Id
    ){
        List<BaseAttrInfo> baseAttrInfos=baseAttrInfoService.getAttrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(baseAttrInfos);
    }

    @ApiOperation("根据平台属性ID获取平台属性对象数据")
    @GetMapping("/getAttrValueList/{attrId}")
    public Result<List<BaseAttrValue>> getAttrValueList(
            @ApiParam(value = "平台属性Id",required = true)
            @PathVariable("attrId") Long attrId
    ){
        QueryWrapper<BaseAttrValue> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("attr_id",attrId);
        List<BaseAttrValue> list = baseAttrValueService.list(queryWrapper);
        return Result.ok(list);
    }
    @ApiOperation("保存与修改平台属性名和值")
    @PostMapping("/saveAttrInfo")
    public Result<BaseAttrInfo> saveOrUpdateAttrInfo(
            @ApiParam(value = "平台属性名和值",required = true)
            @RequestBody BaseAttrInfo baseAttrInfo
    ){
        baseAttrInfoService.saveOrUpdateInfoAndValue(baseAttrInfo);
        return Result.ok();
    }

}
